package org.agent.aigccore.basic.validation;

import org.agent.aigccore.basic.exception.BusinessException;
import org.agent.aigccore.basic.exception.ResultCode;
import org.agent.aigccore.utils.FormatUtil;
import org.agent.aigccore.utils.StringUtil;
import org.apache.commons.collections4.MapUtils;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
 * 断言工具类
 */
public class AssertUtils {

    /**
     * 断言为真
     *
     * @param expression
     */
    public static void assertTrue(boolean expression) {
        assertTrue(expression, ResultCode.PARAM_ERROR.getDesc());
    }

    /**
     * 断言为真
     *
     * @param expression
     * @param message
     */
    public static void assertTrue(boolean expression, String message) {
        if (!expression) {
            throw new BusinessException(ResultCode.PARAM_ERROR, message);
        }
    }

    /**
     * 断言为真
     *
     * @param expression
     * @param resultCode
     * @param params
     */
    public static void assertTrue(boolean expression, ResultCode resultCode
            , String message, Object... params) {
        if (!expression) {
            throw new BusinessException(resultCode, FormatUtil.format(message, params));
        }
    }

    /**
     * 断言为空
     *
     * @param obj
     */
    public static void assertNull(Object obj) {
        if (Objects.nonNull(obj)) {
            throw new BusinessException(ResultCode.PARAM_ERROR, "参数必须为空");
        }
    }

    /**
     * 断言不为空
     *
     * @param obj
     */
    public static void assertNotNull(Object obj) {
        assertNotNull(obj, "参数不能为空");
    }

    /**
     * 断言不为空
     *
     * @param obj
     * @param message
     */
    public static void assertNotNull(Object obj, String message) {
        if (Objects.isNull(obj)) {
            throw new BusinessException(ResultCode.PARAM_ERROR, message);
        }
    }

    /**
     * 断言不为空
     *
     * @param str
     */
    public static void assertNotBlank(String str) {
        assertNotBlank(str, "参数不能为空");
    }

    /**
     * 断言不为空
     *
     * @param str
     * @param message
     */
    public static void assertNotBlank(String str, String message) {
        if (StringUtil.isBlank(str)) {
            throw new BusinessException(ResultCode.PARAM_BLANK, message);
        }
    }

    /**
     * 断言相等
     *
     * @param obj1
     * @param obj2
     */
    public static void assertEquals(Object obj1, Object obj2) {
        assertEquals(obj1, obj2, "参数不相等");
    }

    /**
     * 断言相等
     *
     * @param obj1
     * @param obj2
     * @param message
     */
    public static void assertEquals(Object obj1, Object obj2, String message) {
        if (Objects.isNull(obj1)) {
            assertNull(obj2);
        }
        if (!obj1.equals(obj2)) {
            throw new BusinessException(ResultCode.PARAM_ERROR, message);
        }
    }

    /**
     * 断言不为空
     *
     * @param collection
     */
    public static void assertNotEmpty(Collection<?> collection) {
        assertNotEmpty(collection, "参数不能为空");
    }

    /**
     * 断言不为空
     *
     * @param collection
     * @param message
     */
    public static void assertNotEmpty(Collection<?> collection, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new BusinessException(ResultCode.PARAM_ERROR, message);
        }
    }

    /**
     * 断言不为空
     *
     * @param collection
     * @param message
     * @param params
     */
    public static void assertNotEmpty(Collection<?> collection, String message, Object... params) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new BusinessException(ResultCode.PARAM_ERROR, FormatUtil.format(message, params));
        }
    }

    /**
     * 断言不为空
     *
     * @param map
     * @param message
     */
    public static void assertNotEmpty(Map<?, ?> map, String message) {
        assertNotEmpty(map, ResultCode.PARAM_ERROR, message);
    }

    /**
     * 断言不为空
     *
     * @param map
     * @param resultCode
     * @param message
     */
    public static void assertNotEmpty(Map<?, ?> map, ResultCode resultCode, String message) {
        if (MapUtils.isEmpty(map)) {
            throw new BusinessException(resultCode, message);
        }
    }

    /**
     * 断言不为空
     *
     * @param map
     * @param message
     * @param params
     */
    public static void assertNotEmpty(Map<?, ?> map, String message, Object... params) {
        assertNotEmpty(map, ResultCode.PARAM_ERROR, message, params);
    }

    /**
     * 断言不为空
     *
     * @param map
     * @param resultCode
     * @param message
     * @param params
     */
    public static void assertNotEmpty(Map<?, ?> map, ResultCode resultCode, String message, Object... params) {
        if (MapUtils.isEmpty(map)) {
            throw new BusinessException(resultCode, FormatUtil.format(message, params));
        }
    }
}
